/*!
 * 我的视频选择模块
 * @author Sherlock 
 * ------------------------------
 * videoChooseCallback 视频选择后的回调
 */
require( './style.less' );
define( [ 'jquery', 'require', 'message', 'bootstrap', 'api', 'bigwe_utils' ], function( $, require ) {
  var returnParam = {};

  var ShowTitleMessage = require( 'message' );
  var showTime = ShowTitleMessage.showTime;
  var BIGWE_API = require( 'api' );
  var $me = returnParam.$selector = $( '#my_video_choose' );
  var searchParam = {
    page_num: 1,
    done: 'video'
  };

  var isLoading = 0,
    isNoData = 0;

  var result = [];

  $me.on( 'shown.bs.modal', function() {
    if ( $( '#video_list .video_detail', this ).length ) return;
    ajaxRequest();

  } ).on( 'click', '.video_detail', function() {

    $( this ).addClass( 'selected' ).siblings().removeClass( 'selected' );

  } ).find( '#video_sure' ).on( 'click', function() {

    var selected = $( '.video_detail.selected', $me );

    if ( selected.length ) {

      if ( typeof returnParam.videoChooseCallback === 'function' ) {
        returnParam.videoChooseCallback.call( $me, {
          'videoObj': result[ selected.index() ],
          'videoThumb': $( '.video_thumb', selected ).attr( 'src' )
        } );
      }
      $me.modal( 'hide' );
    } else {
      ShowTitleMessage.showWarning( '请选择一个视频', showTime );
    }
  } );

  $( '#video_list', $me ).scrollMax( function() {

    if ( !isLoading && !isNoData ) {

      searchParam.page_num++;
      ajaxRequest();

    }

  } );

  function ajaxRequest( fn ) {
    isLoading = 1;
    $( '.spinner', $me ).show();
    $.ajax( {
      type: 'post',
      url: BIGWE_API.COMMON.GET_MATERIAL_MPNEWS,
      data: searchParam,
      success: function( data ) {
        data = typeof data === 'string' ? $.parseJSON( data ) : data;
        if ( data.ret ) {
          ShowTitleMessage.showWarning( data.msg || '未知的错误', showTime );
          return;
        }
        if ( !data.length ) {
          ShowTitleMessage.showWarning( '没有数据了', showTime );
          isNoData = 1;
          return;
        }
        result = result.concat( data );
        var appendHtml = '';

        for ( i in data ) {
          $( '#video_list', $me ).append( getVideoHtml( data[ i ] ) );
        }

        if ( fn ) {
          fn.call( $me );
        }

      },
      complete: function() {
        isLoading = 0;
        $( '.spinner', $me ).hide();
      }
    } );
  }

  function getVideoHtml( video ) {

    var html = '';
    html += '<div class="richvideo_content video_detail">';
    html += '<p class="title">' + ( video.title || '' ) + '</p>';
    html += '<p class="time hide"></p>';
    html += '<div class="video_extra_info">';
    html += '<img class="video_thumb" onerror="this.src=\'Public/static/images/wuyulantu.png\'" src="' + video.address1 + '">';
    html += '<span class="video_length hide">0:09</span>';
    html += '</div>';
    html += '<div class="video_desc">' + ( video.synopsis || '没有简介' ) + '</div>';
    html += '<div class="mask"><span class="glyphicon glyphicon-ok ok_icon"></span></div>';
    html += '</div>';

    return html;
  }

  return returnParam;

} );
